
package work_pattern.routing;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import work_pattern.Constant;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer2 {
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        Channel channel = Constant.createChannel();
        channel.queueDeclare(Constant.ROUTING_QUEUE_2, true, false, false, null);
        DefaultConsumer consumer = new DefaultConsumer(channel) {
            /**
             *
             * @param consumerTag 消费者标签，通常是消费者在订阅队列时指定的
             * @param envelope 消息的封包信息(队列名称、交换机...)
             * @param properties 配置
             * @param body 消息内容
             * @throws IOException
             */
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("接收到消息: " + new String(body));
            }
        };

        channel.basicConsume(Constant.ROUTING_QUEUE_2, true, consumer);
        // 等待回调函数执行完毕
        Thread.sleep(10000);
        // 释放资源
        channel.close();
    }
}
